package com.guigu.qingcheng.systemservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.pojo.system.TbLoginLog;
import com.guigu.qingcheng.systemservice.mapper.TbLoginLogMapper;
import com.guigu.qingcheng.systemservice.service.TbLoginLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class TbLoginLogServiceImpl extends ServiceImpl<TbLoginLogMapper, TbLoginLog> implements TbLoginLogService {
    @Autowired
    private TbLoginLogService tbLoginLogService;

    /**
     * 条件查询
     * @param searchMap， 查询所带条件
     * @return
     */
    @Override
    public List<TbLoginLog> findList(Map<String, Object> searchMap) {
        QueryWrapper<TbLoginLog> wapper = queryWrapper(searchMap);
        List<TbLoginLog> list = tbLoginLogService.list(wapper);
        return list;
    }

    /**
     * 分页条件查询
     * @param page 页号
     * @param size 页大小
     * @param searchMap 查询所带条件
     * @return
     */
    @Override
    public Page<TbLoginLog> findPage(Integer page, Integer size, Map<String, Object> searchMap) {
        Page<TbLoginLog> pg = new Page<TbLoginLog>(page, size);
        QueryWrapper<TbLoginLog> wapper = queryWrapper(searchMap);
        IPage<TbLoginLog> list = tbLoginLogService.page(pg, wapper);
        return pg;
    }

    /**
     * 高级组合查询封装对象
     * @param searchMap
     * @return
     */
    public QueryWrapper<TbLoginLog> queryWrapper(Map<String, Object> searchMap){
        QueryWrapper<TbLoginLog> wapper = new QueryWrapper<TbLoginLog>();
        if(searchMap != null){
            if(searchMap.get("id") != null && !"".equals(searchMap.get("id")))
                wapper.eq("id", searchMap.get("id"));
            if(searchMap.get("loginName") != null && !"".equals(searchMap.get("loginName")))
                wapper.like("login_name", searchMap.get("loginName"));
            if(searchMap.get("ip") != null && !"".equals(searchMap.get("ip")))
                wapper.eq("ip", searchMap.get("ip"));
            if(searchMap.get("browserName") != null && !"".equals(searchMap.get("browserName")))
                wapper.like("browser_name", searchMap.get("browser_name"));
            if(searchMap.get("location") != null && !"".equals(searchMap.get("location")))
                wapper.like("location", searchMap.get("location"));
            if(searchMap.get("startTime") != null && !"".equals(searchMap.get("startTime")))
                wapper.ge("login_time", searchMap.get("startTime"));
            if(searchMap.get("endTime") != null && !"".equals(searchMap.get("endTime")))
                wapper.le("login_time", searchMap.get("endTime"));
        }
        wapper.orderByDesc("login_time");
        return wapper;
    }
}
